#include "SysCommons.h"
#include <istream>
#include "Utils.h"

bool Utils::isWhitespace(char c) {
	return c == '\n' || c == ' ' || c == '\t';
}

bool Utils::isWhitespace(const std::string &str) {
	for(size_t i = 0 ; i < str.length() ; ++i) {
		char c = str[i];
		if(!isWhitespace(c)) return false;
	}

	return true;
}

bool Utils::read(std::istream &in_stream, char &c) {
	if(!in_stream.eof()) {
		in_stream >> c;
		return false;
	}

	return true;
}

bool Utils::read(std::istream &in_stream, std::string &str) {
	char buffer[1024];
	if(!in_stream.eof()) {
		in_stream >> buffer;
		str = buffer;
		return false;
	}

	return true;
}

int Utils::stringToInt(const std::string &str) {
	int v = std::numeric_limits<int>::signaling_NaN();

	std::istringstream(str) >> v;
	if(v != std::numeric_limits<int>::signaling_NaN()) {
		std::string temp = str;
		std::reverse(temp.begin(), temp.end());
		int chk = std::numeric_limits<int>::signaling_NaN();
		std::istringstream(str) >> chk;
		if(chk == std::numeric_limits<int>::signaling_NaN()) {
			return chk;
		}
	}

	return v;
}

std::string Utils::intToString(int num) {
	std::stringstream strbuf;
	strbuf << num;
	return strbuf.str();
}